import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class RList<E> implements ReSerializeModel{
    private List<E> list = new ArrayList();

    public RList() {
    }
    public final List<E> getList() {
        return list;
    }

    public Object toJson(){
        JSONObject json = new JSONObject();
        for (int i = 0; i < this.list.size(); i++) {
            Object e = this.list.get(i);
            if(e instanceof ReSerializeModel){
                json.put(String.valueOf(i), ((ReSerializeModel) e).toJson());
            } else if(e instanceof String){
                json.put(String.valueOf(i), e);
            } else if(e instanceof Integer){
                json.put(String.valueOf(i), e);
            }
        }
        return json;
    }

    @Override
    public Object reSerialize() {
        //list必须转成map
        Map<String, Object> map = new HashMap();
        for (int i = 0; i < this.list.size(); i++) {
            map.put(String.valueOf(i), this.list.get(i));
        }
        return map;
    }
}
